<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    【react】Fiber &amp; Diff &amp; HOC |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-fiber &amp; diff &amp; HOC"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  【react】Fiber &amp; Diff &amp; HOC
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2023/10/21/fiber%20&%20diff%20&%20HOC/" class="article-date">
  <time datetime="2023-10-20T23:21:42.000Z" itemprop="datePublished">2023-10-21</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/react/">react</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">1.4k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">4 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/gaASCIVTuUfsM-cVP2teGw">原文</a> | <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1y94y1v7TL?p=10&vd_source=a56b31fb136d9787929c44f3b4e80c4f">视频</a></p>
<h1 id="fiber架构"><a href="#fiber架构" class="headerlink" title="fiber架构"></a>fiber架构</h1><blockquote>
<p>jsx -&gt; babel （转换成虚拟dom） -&gt; render (渲染到页面上，将vDom挂载到真实的dom)</p>
</blockquote>
<h2 id="为什么需要fiber"><a href="#为什么需要fiber" class="headerlink" title="为什么需要fiber"></a>为什么需要fiber</h2><p>vdom -&gt; dom （renderer 渲染器</p>
<p>reconciler diff vdom进行对比更新 （vdom-&gt;dom 是同步的过程</p>
<p>在 16 之前，<code>React</code> 是直接递归渲染 <code>vdom</code> 的，<code>setState</code> 会触发重新渲染，对比渲染出的新旧 <code>vdom</code>，对差异部分进行 <code>dom</code> 操作。</p>
<p>在 16 之后，为了优化性能，会先把 <code>vdom</code> 转换成 <code>fiber</code>，也就是从<code>树转换成链表</code>，然后再渲染。整体渲染流程分成了两个阶段：</p>
<ul>
<li><p><code>render</code> 阶段：从 <code>vdom</code> 转换成 <code>fiber</code>，并且对需要 <code>dom</code> 操作的节点打上 <code>effectTag</code> 的标记</p>
</li>
<li><p><code>commit</code> 阶段：对有 <code>effectTag</code> 标记的 <code>fiber</code> 节点进行 <code>dom</code> 操作，并执行所有的 <code>effect</code> 副作用函数。</p>
</li>
</ul>
<p>从 <code>vdom</code> 转成 <code>fiber</code> 的过程叫做 <code>reconcile</code>（调和），这个过程是<code>可以打断</code>的，由 <code>scheduler</code> 调度执行</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e3976e170f634ab4a8a79583b47236c1~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp"></p>
<p><strong>fiber就是解决同步更新耗时长的问题的</strong></p>
<h3 id="同步更新存在的问题-gt-怎么该进"><a href="#同步更新存在的问题-gt-怎么该进" class="headerlink" title="同步更新存在的问题 -&gt; 怎么该进"></a>同步更新存在的问题 -&gt; 怎么该进</h3><ol>
<li>同步不可中断 -&gt; 异步可中断</li>
<li>scheduler 中存在任务优先级</li>
</ol>
<p>fiber -&gt; <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Window/requestIdleCallback">window.requestIdleCallback()</a>  解决该方法兼容性问题</p>
<blockquote>
<p><code>window.requestIdleCallback(callack,option)</code> 方法插入一个函数，这个函数将在浏览器空闲时期被调用。这使开发者能够在主事件循环上执行后台和低优先级工作，而不会影响延迟关键事件，如动画和输入响应。函数一般会按先进先调用的顺序执行，然而，如果回调函数指定了执行超时时间timeout，则有可能为了在超时前执行函数而打乱执行顺序。</p>
</blockquote>
<h3 id="解决的问题"><a href="#解决的问题" class="headerlink" title="解决的问题"></a>解决的问题</h3><ol>
<li>新的任务调度，有高优先级任务的时候将浏览器让出来，等浏览器空了在执行</li>
<li>新的数据结构，可以随时中断，下次进来可以接着执行</li>
</ol>
<p>节点 -&gt; 子节点 -&gt; 子节点 -&gt; 兄弟节点 -&gt; return父节点</p>
<h1 id="diff算法"><a href="#diff算法" class="headerlink" title="diff算法"></a>diff算法</h1><p><code>diff</code> 算法作用在 <code>reconcile</code> 阶段：</p>
<ul>
<li>第一次渲染不需要 <code>diff</code>，直接 <code>vdom</code> 转 <code>fiber</code>。</li>
<li>再次渲染的时候，会产生新的 <code>vdom</code>，这时候要和之前的 <code>fiber</code> 做下对比，决定怎么产生新的 <code>fiber</code>，<code>对可复用</code>的节点打上<code>修改的标记</code>，剩余的<code>旧节点</code>打上<code>删除标记</code>，<code>新节点</code>打上<code>新增标记</code>。</li>
</ul>
<h2 id="那为什么浏览器里要做-diff"><a href="#那为什么浏览器里要做-diff" class="headerlink" title="那为什么浏览器里要做 diff"></a>那为什么浏览器里要做 diff</h2><blockquote>
<p>因为 dom 创建的性能成本很高，如果不做 dom 的复用，那前端框架的性能就太差了。</p>
</blockquote>
<p>diff 算法的目的就是对比两次渲染结果，找到可复用的部分，然后剩下的该删除删除，该新增新增。</p>
<h2 id="具体实现"><a href="#具体实现" class="headerlink" title="具体实现"></a>具体实现</h2><p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5e84df8ff27e4d62821d33ddb5da70bf~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp"></p>
<p>经过 reconcile 之后，会变成这样的 fiber 结构：<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bc7234f1527c4f87824d61c370acc3b3~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp"></p>
<p>再次渲染出 vdom 的时候，也要进行 vdom 转 fiber 的 reconcile 阶段，但是要尽量能复用之前的节点。</p>
<p>那怎么复用呢？</p>
<p>一一对比下不就行了？</p>
<p>先把之前的 fiber 节点放到一个 map 里，key 就是节点的 key：<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c05c69856a97458fa2152f7d0490d566~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp"></p>
<p>然后每个新的 vdom 都去这个 map 里查找下有没有可以复用的，找到了的话就移动过来，打上更新的 effectTag：<br>**( Q: 为什么标记的是更新 A: 不重新创建 dom，但是属性什么的还是要更新 )**<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f03c4886363642178b7fd39504a35e7b~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp"></p>
<p>这样遍历完 vdom 节点之后，map 里剩下一些，这些是不可复用的，那就删掉，打上删除的 effectTag；如果 vdom 中还有一些没找到复用节点的，就直接创建，打上新增的 effectTag</p>
<p><strong>核心就是找到可复用的节点，删掉剩下的旧节点，新节点新增。</strong></p>
<h1 id="技术总结"><a href="#技术总结" class="headerlink" title="技术总结"></a>技术总结</h1><p>浏览器下使用 <code>react-dom</code> 的渲染器，会先把 <code>vdom</code> 转成 <code>fiber</code>，找到需要更新 <code>dom</code> 的部分，打上增删改的 <code>effectTag</code> 标记，这个过程叫做 <code>reconcile</code>，可以打断，由 <code>scheducler</code> 调度执行。<code>reconcile</code> 结束之后一次性根据 <code>effectTag</code> 更新 <code>dom</code>，叫做 <code>commit</code>。</p>
<p>这就是 react 的基于 fiber 的渲染流程，分成 <code>render（reconcile + schedule）</code>、<code>commit</code> 两个阶段。</p>
<p>当渲染完一次，产生了 <code>fiber</code> 之后，再次渲染的 <code>vdom</code> 要和之前的 <code>fiber</code> 对比下，再决定如何产生新的 <code>fiber</code>，目标是尽可能复用已有的 <code>fiber</code> 节点，这叫做 <code>diff 算法</code>。</p>
<h2 id="所以-React-的-diff-算法是分成两次遍历的："><a href="#所以-React-的-diff-算法是分成两次遍历的：" class="headerlink" title="所以 React 的 diff 算法是分成两次遍历的："></a>所以 React 的 diff 算法是分成两次遍历的：</h2><ul>
<li><p>第一轮遍历，一一对比 <code>vdom</code> 和老的 <code>fiber</code>，如果可以复用就处理下一个节点，否则就结束遍历。</p>
<ul>
<li><p>如果所有的新的 <code>vdom</code> 处理完了，那就把剩下的老 <code>fiber</code> 节点删掉就行。</p>
</li>
<li><p>如果还有 <code>vdom</code> 没处理，那就进行第二次遍历：</p>
</li>
</ul>
</li>
<li><p>第二轮遍历，把剩下的老 <code>fiber</code> 放到 <code>map</code> 里，遍历剩下的 <code>vdom</code>，从 <code>map</code> 里查找，如果找到了，就移动过来。 </p>
<ul>
<li><p>第二轮遍历完了之后，把剩余的老 <code>fiber</code> 删掉，剩余的 <code>vdom</code> 新增。</p>
</li>
<li><p>这样就完成了新的 <code>fiber</code> 结构的创建，也就是 <code>reconcile</code> 的过程。</p>
</li>
</ul>
</li>
</ul>
<h1 id="HOC"><a href="#HOC" class="headerlink" title="HOC"></a>HOC</h1><blockquote>
<p>以组件做为入参并且返回一个新的组件</p>
</blockquote>
<h2 id="使用原因："><a href="#使用原因：" class="headerlink" title="使用原因："></a>使用原因：</h2><ol>
<li>抽离重复代码，实现组件复用</li>
<li>条件渲染渲染拦截</li>
<li>拦截组件的生命周期</li>
</ol>
<h2 id="使用场景："><a href="#使用场景：" class="headerlink" title="使用场景："></a>使用场景：</h2><ol>
<li>属性代理 （模拟修改/操作传入的值）<br>props<br>state  </li>
</ol>
<ol start="2">
<li><p>条件渲染，外部逻辑的封装</p>
</li>
<li><p>反向继承(组件复制类组件)</p>
</li>
<li><p>生命周期的拦截(通过原型链获取生命周期 并绑定this指向)</p>
</li>
</ol>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/10/21/fiber%20&%20diff%20&%20HOC/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/10/21/flvjs/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Flvjs
          
        </div>
      </a>
    
    
      <a href="/2023/04/08/reactQ&A/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Q&amp;A</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "9OLyDF6ymKbs1woL9kh2qbae-gzGzoHsz",
    app_key: "RTkmORaJHqkGjEovHixLK04x",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>